home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacTech 1 to 12
/
MacTech-vol-1-12.toast
/
Tools
/
Alpha 6.51b13 ƒ
/
Tcl
/
UserCode
/
stringListSnips.tcl
< prev
next >
Wrap
Text File
|
1996-08-15
|
2KB
|
73 lines
# Bob Alkire
# Interval Research Corp.
# Other useful code snippets
# substitute string
# in place replaces the characters in str from position "from" to position
# "to" with "rep"
proc substituteString {str from to rep} {
upvar $str lstr
set s [string range $lstr 0 [expr $from - 1]]
append s $rep
append s [string range $lstr [expr $to + 1] end]
set lstr $s
}
# stringTail
# return n characters from the end of string to the end of the string
proc stringTail {n str} {
set len [string length $str]
return [string range $str [expr $len - $n] end]
}
# extractList
# return the pos'th item from theList while removing the item from theList
proc extractList {theList pos} {
upvar $theList lst
if {$pos < 0} {return ""}
set extracted [lindex $lst $pos]
set last [expr [llength $lst] - 1]
if {$pos == 0} then {
set lst [lrange $lst 1 end]
} elseif {$pos == $last} then {
set lst [lrange $lst 0 [expr $last - 1]]
} else {
set lst [concat [lrange $lst 0 [expr $pos -
1]] [lrange $lst [expr $pos + 1] end]]
}
return $extracted
}
# uniqueList
# return a new list with all duplicate items removed from theList
proc uniqueList {theList} {
set last {}
set newList {}
foreach item [lsort $theList] {
if {$last != $item} {set last $item; lappend newList $last}
}
return $newList
}
# diffList
# return a new list that is the set of items not in list1 and list2
proc diffList {list1 list2} {
set diff {}
foreach item $list1 {
if {[lsearch $list2 $item] == -1} {lappend diff $item}
}
return $diff
}
# sliceList
# theList must be a list of lists
# returns a new list that is made up of a list of the pos'th item in each
list of theList
# example sliceList {{a b c} {d e f} {x y z}} 1 returns b e y
proc sliceList {theList pos} {
set newList {}
foreach item $theList {
lappend newList [lindex $item $pos]
}
return $newList
}